Flutter / GetX Steps / Step7: Screen - Bottom NavBar
Screen -BottomNavBar
-
Steps
1. BottomNavBar widget
path: lib/widgets/bottom_nav.dart
Create widget class
create build()class BottomNavBar extends StatelessWidget { }
add BottomNavigationBarWidget build(BuildContext context) { }
include getxController in the widgetWidget build(BuildContext context) { return Container( child: BottomNavigationBar( items: const [ BottomNavigationBarItem( icon: Icon(Icons.search), label: 'Browse', ), BottomNavigationBarItem( icon: Icon(Icons.history), label: 'History', ), BottomNavigationBarItem( icon: Icon(Icons.settings), label: 'Settings', ), ], currentIndex: controller.currentIndex.value, selectedItemColor: Colors.pink, onTap: controller.changePage, ), ); }
Complete codefinal LoginController controller = Get.put(LoginController()); import 'package:flutter/material.dart'; import 'package:vera/controllers/home_controller.dart'; import 'package:vera/controllers/login_controller.dart'; import 'package:get/get.dart'; import 'package:vera/controllers/login_controller.dart'; class BottomNavBar extends StatelessWidget { @override final LoginController controller = Get.put(LoginController()); Widget build(BuildContext context) { return Container( child: BottomNavigationBar( items: const [ BottomNavigationBarItem( icon: Icon(Icons.search), label: 'Browse', ), BottomNavigationBarItem( icon: Icon(Icons.history), label: 'History', ), BottomNavigationBarItem( icon: Icon(Icons.settings), label: 'Settings', ), ], currentIndex: controller.currentIndex.value, selectedItemColor: Colors.pink, onTap: controller.changePage, ), ); } } currentIndex & onTap are defined in GetXController (LoginController) 2. GetXController
path: lib/controllers/login_controller.dart
define variables and function used in the widgetvar currentIndex = 0.obs; final pages = ['/home', '/login']; void changePage(int index) { currentIndex.value = index; //Get.toNamed(pages[index], id: 1); Get.offNamed(pages[index]); } changePage() is sued for tab switching Complete codeimport 'package:get/get.dart'; import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:vera/models/user_model.dart'; class LoginController extends GetxController { var currentIndex = 0.obs; final pages = ['/home', '/login']; void changePage(int index) { currentIndex.value = index; //Get.toNamed(pages[index], id: 1); Get.offNamed(pages[index]); } } 3. screen
We can add the bottom_nav widget whereever we want path: lib/views/home_page.dart
Add BottomNavBar widget
Complete codebottomNavigationBar:BottomNavBar() import 'package:flutter/material.dart'; import 'package:vera/controllers/home_controller.dart'; import 'package:get/get.dart'; import 'package:vera/widgets/drawer.dart'; import 'package:vera/widgets/bottom_nav.dart'; class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return GetBuilder ( builder: (controller) => Scaffold( appBar: AppBar( title: const Text('Home'), ), body: const Center( child: Text('Hello World!'), ), drawer: NavDrawer(), bottomNavigationBar:BottomNavBar() ), ); } }